﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.UI.MobileControls;
using System.Collections.Generic;

namespace YXShop.Web.admin.shop
{
    public partial class shop_edit : System.Web.UI.Page
    {
        YXShop.BLL.Shop.Shop shopBll = new YXShop.BLL.Shop.Shop();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                InitWebControl();
                string id = ChangeHope.WebPage.PageRequest.GetQueryString("shopId");
                if (id != null && id != "")
                {
                    YXShop.Common.PromptInfo.Popedom("013003004", "对不起，您没有权限进行编辑");
                    ViewState["shopId"] = id;
                    BindShopById(Convert.ToInt32(id));
                }
            }
        }

        #region 初始化
        protected void BindShopById(int shopId)
        {
            YXShop.Model.Shop.Shop shop = shopBll.GetModelById(shopId);
            if(shop!=null)
            {
                this.lblName.Text = shop.Shopname.ToString();
                this.lblRegistTime.Text = shop.Addtime.ToString();
                this.lblGloryNum.Text = shop.Prestigerank.ToString();
                this.lblVisitNum.Text = shop.Visitno.ToString();
                this.txtName.Text = shop.Shopname.ToString();
                YXShop.BLL.Member.MemberInfo infoBll = new YXShop.BLL.Member.MemberInfo();
                YXShop.Model.Member.MemberInfo info = infoBll.GetModel(Convert.ToInt32(shop.Uid));
                YXShop.BLL.Member.MemberAccount accountBll=new YXShop.BLL.Member.MemberAccount();
                if (info != null)
                {
                    this.lblPhone.Text = info.HomePhone.ToString() == "" ? "暂未填写" : info.HandPhone.ToString();
                    this.lblmobile.Text = info.MobilePhone.ToString() == "" ? "暂未填写" : info.MobilePhone.ToString();
                    this.lblAddress.Text = info.Address.ToString() == "" ? "暂未填写" : info.Address.ToString();
                    this.lblPostalcode.Text = info.Zip.ToString() == "" ? "暂未填写" : info.Zip.ToString();                   
                }
                YXShop.Model.Member.MemberAccount account = accountBll.GetModel(Convert.ToInt32(shop.Uid));
                if(account!=null)
                {
                    this.lblEmail.Text = account.Email.ToString() == "" ? "暂未填写" : account.Email.ToString();
                }
                this.ddlRank.SelectedValue = shop.Shoprank.ToString();
                this.ddlWork.SelectedValue = shop.Shoptype.ToString();
                this.txtCredit.Text = shop.Prestigerank.ToString();
                this.txtVisitNum.Text = shop.Visitno.ToString();
                this.lblState.Text = shop.State.ToString() == "1" ? "是" : "否";
                string[] arr = shop.Attribute.Split(',');
                int state = ChangeHope.Common.ValidateHelper.IsNumber(shop.State.ToString()) ? Convert.ToInt32(shop.State) : 0;
                BindCheckBoxList(ChbWork,arr,state);
            }
        }
        #endregion

        #region 验证
        private void InitWebControl()
        {
            BindGrade();
            ChangeHope.WebPage.WebControl.Validate(this.txtCredit,"店铺信誉积分必须是数字，为得真实数据建议不修改","isint","必填","该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtVisitNum,"店铺访问次数必须是数字，为得真实数据建议不修改","isint","必填","该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtName, "店铺名称不能为空，建议不修改好让店铺出名", "isnull", "必填", "该项为必填项");
            this.Form.Attributes.Add("onsubmit", "return CheckForm()");
        }
        #endregion

        #region 绑定
        private void BindGrade()
        {
            YXShop.BLL.Shop.ShopLevel levelBll = new YXShop.BLL.Shop.ShopLevel();
            List<YXShop.Model.Shop.ShopLevel> levelList = levelBll.GetAll();
            if(levelList!=null)
            {
                this.ddlRank.DataSource = levelList;
                this.ddlRank.DataTextField = "Name";
                this.ddlRank.DataValueField = "ID";
                this.ddlRank.DataBind();
            }
            YXShop.BLL.Shop.ShopCategory cateBll = new YXShop.BLL.Shop.ShopCategory();
            List<YXShop.Model.Shop.ShopCategory> cateList = cateBll.GetAll();
            if(cateList!=null)
            {
                this.ddlWork.DataSource = cateList;
                this.ddlWork.DataTextField = "Name";
                this.ddlWork.DataValueField = "ID";
                this.ddlWork.DataBind();
            }
        }
        #endregion

        #region 初始复选框
        private void BindCheckBoxList(CheckBoxList cbl, string[] arr, int bl)
        {
            if (cbl.Items.Count == 7)
            {
                if (arr.Length != 6)
                    return;
                for (int i = 0; i < cbl.Items.Count; i++)
                {
                    if (i < 3)//012
                    {
                        if (Convert.ToInt32(arr[i]) > 0)
                            cbl.Items[i].Selected = true;
                        else
                            cbl.Items[i].Selected = false;
                    }
                    else if (i == 3)//3
                    {
                        if (bl == 1)
                            cbl.Items[i].Selected = true;
                        else
                            cbl.Items[i].Selected = false;
                    }
                    else//4,5
                    {
                        if (Convert.ToInt32(arr[i - 1]) > 0)
                            cbl.Items[i].Selected = true;
                        else
                            cbl.Items[i].Selected = false;
                    }
                }
            }
        }

        #endregion

        #region 获取复选
        private string getCheckBoxListValue(CheckBoxList cbl)
        {
            string reStr = "";
            if (cbl.Items.Count == 7)
            {
                for (int i = 0; i < cbl.Items.Count; i++)
                {
                    if (i == 3)
                    {
                        continue;
                    }
                    reStr += cbl.Items[i].Selected ? "1," : "0,";
                }
                reStr = reStr.TrimEnd(',');
            }
            return reStr;
        }
        #endregion

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        #region  保存
        /// <summary>
        /// 保存
        /// </summary>
        private void Save()
        {
            YXShop.Model.Shop.Shop shop = shopBll.GetModelById(Convert.ToInt32(ViewState["shopId"].ToString()));
            shop.Shoprank = Convert.ToInt32(this.ddlRank.SelectedValue);
            shop.Shoptype = Convert.ToInt32(this.ddlWork.SelectedValue);
            shop.Prestigerank = Convert.ToInt32(this.txtCredit.Text.Trim());
            shop.Visitno = Convert.ToInt32(this.txtVisitNum.Text.Trim());
            shop.State = ChbWork.Items[3].Selected ? 1:0;
            shop.Attribute = getCheckBoxListValue(ChbWork);
            shop.Shopname = this.txtName.Text.Trim().ToString();
            int count = shopBll.Update(shop);
            if (count > 0)
            {
                this.ltlMsg.Text = "操作成功，已修改该信息";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionOk";
            }
            else
            {
                this.ltlMsg.Text = "操作失败";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionErr";
            }

        }
        #endregion

    }
}
